﻿using BUS;
using DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineDigitalCameraWebsite
{
    public partial class DanhMuc : System.Web.UI.Page
    {
        private static List<Camera> listCamera;
        private static List<Accessory> listAccessory;
        private static List<Lens> listLens;

        public static List<Accessory> ListAccessory
        {
            get { return DanhMuc.listAccessory; }
            set { DanhMuc.listAccessory = value; }
        }
        
        public static List<Lens> ListLens
        {
            get { return DanhMuc.listLens; }
            set { DanhMuc.listLens = value; }
        }

        public static List<Camera> ListCamera
        {
            get { return listCamera; }
            set { listCamera = value; }
        }

        protected void btnAddCart_Click(object sender, EventArgs e)
        {
            LinkButton bti = (LinkButton)sender; 
            string cate = "camera"; 

            Label txtID = (Label)bti.Parent.FindControl("lblID");
            int id = int.Parse(txtID.Text);

            Image imghinh = (Image)bti.Parent.FindControl("imghinh");
            Session["lblimage"] = imghinh.ImageUrl;
            Response.Redirect("/ThemVaoGioHang.aspx?cate=" + cate + "&ID=" + id);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string cate;
                try
                {
                     
                    // lay danh sach tat ca
                    if (listLens== null)
                    {
                        LensBUS lBus = new LensBUS();
                        listLens = lBus.GetListAll();
                    }
                    if (listCamera == null)
                    {
                        CameraBUS cBUS = new CameraBUS();
                        listCamera = cBUS.GetListAll();
                    }
                    if (listAccessory == null)
                    {
                        AccessoriesBUS aBUS = new AccessoriesBUS();
                        listAccessory = aBUS.GetListAll();
                    }
                    cate = Request.QueryString["cate"].ToString();
                    LoadDuLieu(cate);
                }
                catch
                {
                    cate = "danhmuc";
                    LoadDuLieu(cate);
                }
            }
        }

        private void LoadDuLieu(string cate)
        {
            if (cate == "danhmuc")
            {
                // lay danh sach tat ca
                LoadLens();
                LoadCamera();
                LoadAcc();
            }
            else
            if (cate == "camera")
            {
                // lay danh sach camera 
                LoadCamera();
                
            }
            else if (cate == "lens")
            {
                // lay danh sach lens
                LoadLens();
               
            }
            else if (cate == "accessories")
            {
                // lay danh sach accessories 
                LoadAcc();
                
            }
        }

        private void LoadLens()
        {
            if (listLens != null)
            {
                dtLens.DataSource = listLens;
                dtLens.DataBind();
                lblKetQuaLens.Text = "Lens: có " + listLens.Count + " sản phẩm";
                lblKetQuaLens.Visible = true;
            }
            else
            {
                lblKetQuaLens.Text = "Lens: Không có sản phẩm nào";
                lblKetQuaLens.Visible = true;
            }
        }

        private void LoadAcc()
        {
            if (listAccessory != null)
            {
                dtAcc.DataSource = listAccessory;
                dtAcc.DataBind();
                lblKetQuaAcc.Text = "Accessory: có " + listAccessory.Count + " sản phẩm";
                lblKetQuaAcc.Visible = true;
            }
            else
            {
                lblKetQuaAcc.Text = "Camera: Không có sản phẩm nào";
                lblKetQuaAcc.Visible = true;
            }
        }

        private void LoadCamera()
        {
            if (listCamera != null)
            {
                dtCamera.DataSource = listCamera;
                dtCamera.DataBind();
                lblKetQuaCamera.Text = "Camera: có " + listCamera.Count + " sản phẩm";
                lblKetQuaCamera.Visible = true;
            }
            else
            {
                lblKetQuaCamera.Text = "Camera: Không có sản phẩm nào";
                lblKetQuaCamera.Visible = true;
            }
        }
         

        protected void btnAddCartAcc_Click(object sender, EventArgs e)
        {
            LinkButton bti = (LinkButton)sender;
            string cate = "accessories";

            Label txtID = (Label)bti.Parent.FindControl("lblID");
            int id = int.Parse(txtID.Text);

            Image imghinh = (Image)bti.Parent.FindControl("imghinh");
            Session["lblimage"] = imghinh.ImageUrl;
            Response.Redirect("/ThemVaoGioHang.aspx?cate=" + cate + "&ID=" + id);
        }

        protected void btnAddCartLens_Click(object sender, EventArgs e)
        {
            LinkButton bti = (LinkButton)sender;
            string cate = "lens";

            Label txtID = (Label)bti.Parent.FindControl("lblID");
            int id = int.Parse(txtID.Text);

            Image imghinh = (Image)bti.Parent.FindControl("imghinh");
            Session["lblimage"] = imghinh.ImageUrl;
            Response.Redirect("/ThemVaoGioHang.aspx?cate=" + cate + "&ID=" + id);
        }
    }
}